<template>
  <el-dialog
    :title="showTitle"
    width="50%"
    center
    :visible="showDialog"
    @close="btnCancel"
  > 
    <el-form :model="formData" :rules="rules" label-width="100px" ref="deptForm">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select @focus="getEmployeeSimple" v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" type="textarea" placeholder="1-300个字符" :rows="3"></el-input>
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      // 获取最新的数据
      const { depts } =  await getDepartments()
      let isRepeat = false
      if(this.formData.id) {
        // 编辑子部门
       isRepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.formData.pid).some(item => item.name === this.formData.name)
      } else {
        // 新增部门
       isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }
      isRepeat ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if(this.formData.id) {
        // 编辑部门
        isRepeat = depts.filter(item => item.id !== this.formData.id).some(item => item.code === value)
      } else {
        // 新增部门
        isRepeat = depts.some(item => item.code === value && value)
      }
      isRepeat ? callback(new Error(`组织架构中已经有部门使用${value}编码`)) : callback() 
    }
    return {
      // 定义表单数据
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      // 定义表单校验规则
      rules: {
        name: [{required: true, message: '部门名称不能为空', trigger: 'blur'}, {min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur'}, {trigger: 'blur', validator: checkNameRepeat}],
        code: [{required: true, message: '部门编码不能为空', trigger: 'blur'}, {min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur'}, {trigger: 'blur', validator: checkCodeRepeat}],
        manager: [{required: true, message: '部门负责人不能为空', trigger: 'blur'}],
        introduce: [{required: true, message: '部门介绍不能为空', trigger: 'blur'}, {min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur'}]
      },
      peoples: [] // 接收获取的员工简单列表的数据
    }
  },
  methods: {
    // 获取员工简单列表数据
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    btnOK() {
      this.$refs.deptForm.validate(async(isok) => {
        if(isok) {
          // 判断是否新增和编辑
          if(this.formData.id) {
            // 编辑部门
            await updateDepartments(this.formData)
          } else {
            await addDepartments({...this.formData, pid: this.treeNode.id})
          }
          // 添加成功后更新页面数据
          this.$emit('addDepts')
          // 关闭弹出层
          this.$emit('update:showDialog', false)
        }
      })
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.deptForm.resetFields()
    },
    // 根据ID查询部门详情
    async getDepartDetail(id) {
      // 编辑部门数据实现数据回写
      this.formData = await getDepartDetail(id)
    }
  }
}
</script>

<style>

</style>